﻿@{
    ViewBag.Title = "TestGettingAndPuttingEntryList";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>TestGettingAndPuttingEntryList</h2>
<input type="text" id="tags" value="abc" />

<button id="changeTag">Change Subentry tag</button>
<button id="changeFolderTag">Change Folder Tag</button>


@section scripts {

    @CoffeeScripts.Render("~/CoffeeScripts/shared/coffee")
    @CoffeeScripts.Render("~/CoffeeScripts/models/coffee")
    @CoffeeScripts.Render("~/CoffeeScripts/controllers/coffee")
    @CoffeeScripts.Render("~/CoffeeScripts/coffee")
    @Scripts.Render("~/Scripts/coffee-script.js", "~/bundles/waypoint")

    <script type="text/coffeescript">

$(document).ready(() ->
  $("#changeFolderTag").click(() =>
    entry = Cofe.Entry.fromUrl(Cofe.utils.formatPath("{cofe}"))
    entry.metadata.tags = $("#tags").val()
    json = entry.toJson(["tags"])
    console.log "toJson:"
    console.log json
    $.ajax({ 
          url : "/api/entry"
          type : "POST"
          data : json
          dataType: "json"
          success: ((data, ts, xhr) => 
              console.log "success"
              entry2 = Cofe.Entry.fromUrl(Cofe.utils.formatPath("{cofe}"))
              console.log entry2
              console.log entry2.metadata.tags   
          )
          error : ((data, ts, xhr) =>
              console.log "failed"
              console.log data
          )
          contentType:"application/json; charset=utf-8"
       })
  )

  $("#changeTag").click(() =>
    eList = Cofe.EntryList.fromUrl(Cofe.utils.formatPath("{cofe}", {"rel" : "list" }))
    eList.bind("fetched", () =>
       for e in eList.entries
          e.metadata.tags = $("#tags").val()
       eList.unbind("fetched")
       json = eList.toJson(["tags"]) #json contains id and tags only
       console.log "toJson:"
       console.log json
       $.ajax({ 
          url : "/api/entryList"
          type : "POST"
          data : json
          dataType: "json"
          success: ((data, ts, xhr) => 
              console.log "success"
              eList2 = Cofe.EntryList.fromUrl(Cofe.utils.formatPath("{cofe}", {"rel" : "list" }))
              console.log eList2
              console.log eList2.entries[0].metadata.tags   #first item
          )
          error : ((data, ts, xhr) =>
              console.log "failed"
              console.log data
          )
          contentType:"application/json; charset=utf-8"
       })
    )
    eList.fetch()
  )
)
    </script>

}